package fit.deni.pages;

import java.util.Collection;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.EventLink;
import org.apache.tapestry5.ioc.annotations.Inject;

import fit.deni.beans.Glumac;
import fit.deni.beans.Zaposleni;
import fit.deni.hibernate.interfaces.DVDService;
import fit.deni.hibernate.interfaces.GlumacService;
import fit.deni.hibernate.interfaces.LogService;

public class Actors {

	@Property
	private Collection<Glumac> glumci;
	@Property
	private Glumac singleGlumac;
	@Persist(PersistenceConstants.FLASH)
	@Property
	private Glumac noviGlumac;

	@Inject
	private GlumacService glumacService;
	@Inject
	private LogService logService;
	@Inject
	private DVDService dvdService;
	
	@Property
	@SessionState
	private Zaposleni korisnik;
	private boolean korisnikExists;

	private Object onActivate() {
		if(!korisnikExists) return Login.class;
		glumci = glumacService.getAll();
		return null;
	}

	private Object onSuccess() {
		glumacService.addActor(noviGlumac);
		noviGlumac = new Glumac();
		return this;
	}
	
	@Component
	private EventLink edit, delete;

	public void onDelete(String id) {
		try {
			glumacService.deleteActor(glumacService.getActorById(Integer.parseInt(id)));
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Here you should handle delete errors");
		}
	}
	
	public void onEdit(String id) {
		try {
			noviGlumac = glumacService.getActorById(Integer.parseInt(id));
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Here you should handle edit errors");
		}
	}
	
	@InjectPage
	private Movies movies;
	
	public Object onSelect(int id) {
		movies.setSelectedDVDs(dvdService.getDVDsFromActor(id));
		return movies;
	}

}
